<?php

/**
 * --------------------------------
 * Default HomeworkController
 *
 * 作业管理的统一类  HomeworkController
 * 
 * --------------------------------
 */

class HomeworkController extends BaseController{

 // /**
 //     * 构造函数
 //     * 统一用户Session验证
 //     * 
 //     */
 //    public function __construct(){
 //    	$check_result = Basecontroller::session('internal');
 //    	if(!$check_result) 
 //    		exit();
 //    }
	/**
	 * 查询作业接口 
	 * @param
	 * @return Response
	 */
	public function index(){
     	
     	$column = Input::get('column');
     	$content = Input::get('content');
     	$result = DB::table('homework');
     	if(null!=$column && ''!= $column && null!= $content && ''!= $content){

			$array = $array->where('homework.'.$column, 'like', '%'.$content.'%');
        }
     	
		$result = $result
					 ->select('*')
					 ->orderBy('created_at','DESC');
					 
		$result = $result->paginate(15)->toJson();			 
		$result = json_decode($result);
		return Response::json(['error'=>false,'result'=>$result],200);
    }

    /**
	 * 作业详情接口 
	 * @param
	 * @return Response
	 */
    public function show($id){
    	$result = DB::table('homework')			 
					 ->select('*')
					 ->where('id',$id)
					 ->get();
		return Response::json(['error'=>false,'result'=>$result],200);			 

    }


    /**
	 * 新增作业接口 
	 * @param 
	 * @return Response
	 */
	public function store(){

        if(!Input::has("classes_id") || !Input::has("subject_id") || !Input::has('content') ||
		   !Input::has('submit_time')){
	 		return Response::json(['error'=>true,'message'=>"参数错误"], 201);
	 	}
	 	$new_homework_arr = array();
		$message = Input::all();
		$homework_message = array('classes_id','subject_id','content',					   			  				  'submit_time','remark');
		foreach ($message as $key => $value) {
			if(in_array($key,$homework_message)){
				$new_homework_arr[$key] = $value;
			}
		}
		$new_homework_arr['publish_time'] = date('Y-m-d');
	 	$id = DB::table('homework')->insertGetId($new_homework_arr);
	 	if($id){
	 		return Response::json(['error'=>false,'message'=>'创建成功','result'=>$id],200);	
	 	}else{
	 		return Response::json(['error'=>true,'message'=>'创建失败'],201);
	 	}
	}

	/**
	 * 修改作业接口 
	 * @param 
	 * @return Response
	 */
	public function update($id){
		$new_homework_arr = array();
		$message = Input::all();
		$homework_message = array('classes_id','subject_id','content',					   			  				  'submit_time','remark');
		foreach ($message as $key => $value) {
			if(in_array($key,$homework_message)){
				$new_homework_arr[$key] = $value;
			}
		}
	 	$id = DB::table('homework')->where('id',$id)->update($new_homework_arr);
	 	if($id){
	 		return Response::json(['error'=>false,'message'=>'修改成功'],200);	
	 	}else{
	 		return Response::json(['error'=>true,'message'=>'修改失败'],201);
	 	}
	}

	}
	
	/**
	 * 删除考试接口 
	 * @param 
	 * @return Response
	 */
	public function destroy($id){

	    $del_result = DB::table('homework')->where('id',$id)->delete();
	    if($del_result){
	    	return Response::json(['error'=>false,'message'=>'删除成功'],200);
	    }else{
	    	return Response::json(['error'=>true,'message'=>'删除失败'],201);
	    }

	}
}	